package com.bt.lms.ui.action.enp;

import com.alibaba.fastjson.JSON;
import com.bt.lms.constants.EnpBizTypeConstant;
import com.bt.lms.entity.EnrollmentPlan;
import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.ui.page.common.action.pageenp.EnpLabAction;
import com.bt.lms.ui.page.enp.DetailEnrollmentPlan;
import com.bt.lms.ui.action.enp.vo.AddEnpVo;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.layout.Pane;
import org.apache.commons.beanutils.BeanUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;

/**
 * 招生计划名称详情事件
 */
public class CheckEnrollmentPlanDetailsAction implements EventHandler {
    @Override
    public void handle(Event event) {

        //1.组包（根据自定义的网络消息协议）
        AddEnpVo addEnpVo = new AddEnpVo(); //课程信息vo
        byte[] bytes = handleReqMsg(addEnpVo);

        //组网
        Socket socket = getNet();

        //发送消息
        boolean flag = sendReq(socket, bytes);


        DetailEnrollmentPlan detailEnrollmentPlan = new DetailEnrollmentPlan();
        Pane pane = EnpLabAction.pageSkipInfo.getPane();
        Integer index = EnpLabAction.pageSkipInfo.getIndex();
        pane.getChildren().set(index, detailEnrollmentPlan);

    }


    /**
     * 发送信息
     * 1.将消息转为字节流
     * 2.发送
     *
     * @param socket 发送的socket
     * @param bytes  要发送的消息
     * @return
     */
    private boolean sendReq(Socket socket, byte[] bytes) {

        try {
            OutputStream out = socket.getOutputStream();
            out.write(bytes);
            out.flush(); //冲刷

            //接收回应信息
            InputStream in = socket.getInputStream();
            byte[] buf = new byte[1024];
            in.read(buf);
            RespMsg respMsg = RespMsg.toRespMsg(buf);
            String data = respMsg.getData();

        } catch (IOException e) {
//            throw new RuntimeException(e);
        }

        return true;
    }


    /**
     * 获取网络
     *
     * @return socket
     */
    private Socket getNet() {

        String serverIp = "127.0.0.1";
        int serverPort = 8888;
        Socket socket = null;

        try {
            socket = new Socket(serverIp, serverPort);
        } catch (IOException e) {
//            throw new RuntimeException(e);
            return null;
        }

        return socket;
    }


    /**
     * 将用户输入的信息处理成我们规定的请求信息
     * 组包
     *
     * @param addEnpVo 用户输入
     * @return 请求消息
     */
    private byte[] handleReqMsg(AddEnpVo addEnpVo) {
        //消息
        ReqMsg reqMsg = new ReqMsg();

        //教学计划信息
        EnrollmentPlan enrollmentPlan = new EnrollmentPlan();

        //业务类型
        reqMsg.setBizType(EnpBizTypeConstant.ENP_PLAN_DETAIL);

        //vo转entity(目标，源)
        try {
            BeanUtils.copyProperties(enrollmentPlan, addEnpVo);
            enrollmentPlan.setId(1L);
            enrollmentPlan.setTeachPlanId(3L);
            String enpStr = JSON.toJSONString(enrollmentPlan);
            //数据带走
            reqMsg.setData(enpStr);

        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        byte[] bytes = reqMsg.toBytes();

        return bytes;
    }
}
